# 此接口用于从orthanc中下载dicom文件夹
# 调用命令行，使用curl命令从服务器上爬数据到指定路径

import os
from database.tools.application_batch_test import db
# from Flask_run import db
from .models.study_id import Study_id
from database.models.study_id import Study_id

         
def get_dicom_from_Orthanc(study_instance_uid):
    # 首先根据study_instance_uid获取study_id
    study_id = db.session.query(Study_id.orthanc_id).filter(Study_id.study_id == study_instance_uid).first()
    # 使用curl命令下载dicom文件到指定路径,命名为study id
    source_path='../Dicom_download/'
    if not os.path.isdir(source_path):
      os.mkdir(source_path)
    save_path="../Dicom_download/"+study_instance_uid+".zip"
    order="curl --insecure --anyauth -u orthanc:orthanc -X GET http://localhost:8042/studies/"+study_id[0]+"/archive > "+save_path
    os.system(order)
    return save_path

    
if __name__ == '__main__':
    study_id='1.2.840.113564.9435757939512.7620.636503042164354276.302'
    get_dicom_from_Orthanc(study_id)
    print('finish')
